﻿using Payroll.Biz.Legacy.DAL;
using Payroll.Biz.Legacy.Models;
using Payroll.Biz.Legacy.ServiceParams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.Biz.Legacy.Services
{
    public class AddTimeCardService : AbstractService, IService<AddTimeCardServiceParams>
    {
        public AddTimeCardService(IPayrollDatabase database) : base(database)
        {
        }

        public ServiceResult Execute(AddTimeCardServiceParams serviceParams)
        {
            Employee employee = Database.GetEmployee(serviceParams.EmployeeId);
            if (employee == null)
            {
                throw CreateServiceException("No such employee.");
            }

            if (!(employee.Classification is HourlyClassification))
            {
                throw CreateServiceException("Tried to add timecard to non-hourly employee");
            }

            Guid id = Guid.NewGuid();

            TimeCard timeCard = new TimeCard(id, employee.Id, serviceParams.Date, serviceParams.Hours);

            Database.AddTimeCard(timeCard);

            return ServiceResult.CreateSuccessResult(timeCard.Id);
        }
    }
}
